package br.com.deployment.manager;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class ImageUploadHandler implements Serializable{

    private static final String INSTANCE_ATTRIBUTE_NAME = "instance";

    private static final String FIELDNAME_ATTRIBUTE_NAME = "fieldName";

//    private static final String NO_IMAGE_PATH = "resources" + File.separator + "images" + File.separator + "no-image.png";

    private byte[] bytes;

    private String hash;

    private ManagerArquivo mi;

    public ImageUploadHandler() {
           mi = new ManagerArquivo();
           
    }
    
    
//    
//    public void handle( FileUploadEvent event ) throws IOException {        
//        handle( event.getFile().getContents() );
//        setField( bytes );
//    }
//
//    public void removeImageClickedHandler( ActionEvent event ) throws IOException {
//        putNoImage();
//        setField( null );
//    }
//
//    public boolean thereIsImage() {
//        Map<String, Object> attributes = getAttributes();
//        Object instance = attributes.get( INSTANCE_ATTRIBUTE_NAME );
//        String fieldName = (String) attributes.get( FIELDNAME_ATTRIBUTE_NAME );
//
////        Field field = ReflectionUtils.findField( instance.getClass(), fieldName );
////        ReflectionUtils.makeAccessible( field );
////        return ReflectionUtils.getField( field, instance ) != null;
//        return true;
//    }
//
//    private void setField( byte[] value ) {
//        Map<String, Object> attributes = getAttributes();
//        Object instance = attributes.get( INSTANCE_ATTRIBUTE_NAME );
//        String fieldName = (String) attributes.get( FIELDNAME_ATTRIBUTE_NAME );
//        
//        mi.getAnexo().setArquivo(bytes);
//        
////        if ( instance == null || fieldName == null )
////            throw new RuntimeException( String.format( "It's necessary give the %s and %s attributes on fileUpload component", INSTANCE_ATTRIBUTE_NAME, FIELDNAME_ATTRIBUTE_NAME ) );
////
////        Field field = ReflectionUtils.findField( instance.getClass(), fieldName );
////        if ( field == null )
////            throw new RuntimeException( String.format( "The field %s not found in class %s", fieldName, instance.getClass() ) );
////
////        ReflectionUtils.makeAccessible( field );
////        ReflectionUtils.setField( field, instance, value );
//    }
//
//    private byte[] getMD5Hash( byte[] contents ) {
//        try {
//            return MessageDigest.getInstance( "MD5" ).digest( contents );
//        } catch ( NoSuchAlgorithmException e ) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//    private void putNoImage() throws IOException {
//        File file = new File( getServletContext().getRealPath( NO_IMAGE_PATH ) );
//        bytes = getBytesFromFile( file );
//        hash = new Gson().toJson( getMD5Hash( bytes ) );
//        ImageUploadServlet.IMAGE_BYTES_BY_HASH.put( hash, bytes );
//    }
//
//    private void handle( byte[] imageBytes ) throws IOException {
//        bytes = imageBytes;
//        hash = new Gson().toJson( getMD5Hash( bytes ) );
//        ImageUploadServlet.IMAGE_BYTES_BY_HASH.put( hash, bytes );
//    }
//
//    private byte[] getBytesFromFile( File file ) throws IOException {
//        if ( file.exists() ) {
//            FileInputStream is = new FileInputStream( file );
//            long length = file.length();
//            byte[] bytes = new byte[(int) length];
//            int offset = 0;
//            int numRead = 0;
//            while ( offset < bytes.length && ( numRead = is.read( bytes, offset, bytes.length - offset ) ) >= 0 ) {
//                offset += numRead;
//            }
//            is.close();
//            return bytes;
//        }
//        return null;
//    }
//
//    private Map<String, Object> getAttributes() {
//        return UIComponent.getCurrentComponent( FacesContext.getCurrentInstance() ).getAttributes();
//    }
//
//    private static ServletContext getServletContext() {
//        return (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
//    }
//
//    public String getHash() throws IOException {
////        if ( hash == null ) {
////            Map<String, Object> attributes = getAttributes();
////            Object instance = attributes.get( INSTANCE_ATTRIBUTE_NAME );
////            String fieldName = (String) attributes.get( FIELDNAME_ATTRIBUTE_NAME );
////
////            boolean putNoImage = true;
////            if ( instance != null && fieldName != null ) {
////                Field field = ReflectionUtils.findField( instance.getClass(), fieldName );
////                if ( field == null )
////                    throw new RuntimeException( String.format( "The field %s not found in class %s", fieldName, instance.getClass() ) );
////
////                ReflectionUtils.makeAccessible( field );
////                Object value = ReflectionUtils.getField( field, instance );
////                if ( value != null && value instanceof byte[] ) {
////                    handle( (byte[]) value );
////                    putNoImage = false;
////                }
////            }
////            if ( putNoImage )
////                putNoImage();
////        }
//        return hash;
//    }
//
//    public void setHash( String hash ) {
//        this.hash = hash;
//    }
//
//    public boolean isThereIsImage() {
//        return thereIsImage();
//    }

}